package com.itheima.autoconfig;

import com.baidu.aip.face.AipFace;
import com.baidu.aip.util.Base64Util;
import lombok.Data;
import org.json.JSONObject;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.HashMap;

/**
 * @Author 请不要叫我高司令
 * @Date 2022/3/14 18:00
 * @Version 1.0
 */


@Data
@Component
@ConfigurationProperties("tanhua.aip")
public class AipFaceTemplate {

    private String appId;
    private String apiKey;
    private String secretKey;


    public boolean detect(byte [] body) {

        AipFace client = new AipFace(appId, apiKey, secretKey);
        // 传入可选参数调用接口
        HashMap<String, String> options = new HashMap<String, String>();
        options.put("face_field", "age");
        options.put("max_face_num", "2");
        options.put("face_type", "LIVE");
        options.put("liveness_control", "NONE");


        String imageType = "BASE64";

        String image = Base64Util.encode(body);

        // 人脸检测
        JSONObject res = client.detect(image, imageType, options);

        int code = res.getInt("error_code");

        return code == 0;
    }
}